<?php 
//---------- config
$manager="QU&#7842;N L&#221; LOGOS - BANNERS";// tieu de trang

$par_page		="sr_catlg";// vi tri trang hien thi trong site

$category       ="catlg".$lang;
$id_category    ="id_catlg";
$item           ="logo".$lang;
$id_item        ="id_logo";

$categoryname 	="nh&#243;m";
$itemname 		="Link - Logo - Banner";
$haveCategory=true;
$haveItem=true;
if($_GET['code'])
{
	$code=$_GET['code'];
}
elseif($_POST['code'])
{
	$code=$_POST['code'];
}
else
{
	$code='00';
}
if($_GET['pid']) 
	$pid=intval($_GET['pid']);
else
	 $pid=0;
//--------lay bien code dieu khien process
$pid=intval($_GET['pid']);
if($_GET['p'])
{
	$p=intval($_GET['p']);
	if($p==0){
		$p=1;
	}
}
if($_GET['pitem'])
{
	$pitem=intval($_GET['pitem']);
	if($pitem==0){
		$pitem=1;
	}
}
$id=intval($_GET['id']);
$tpl=new TemplatePower("skin/sk_catlg.htm");

$tpl->prepare();
$tpl->assignGlobal("cat_name",$categoryname);
$tpl->assignGlobal("item_name",$itemname);
$tpl->assignGlobal("p",$p);
$tpl->assignGlobal("pid",$pid);
$tpl->assignGlobal("manager",$manager);
$tpl->assignGlobal("par_page",$par_page);
$tpl->assignGlobal('del_item_selected',"?page=".$par_page."&code=17&pid=".$pid."&pitem=".$pitem);
$navigator=" <a href='?page=".$par_page."&code=00' class='link_name'>Root></a>";
$dk=intval($_GET['pid']);
$ct=new catlg_tree($dk);
$ct->get_catlg_tree();	
$ct->get_catlg_string_admin($dk);
$navigator.=$catlgstring2;								
$tpl->assignGlobal("pathpage",$navigator);

$tpl->assignGlobal("pid",$pid);
///////////////// category ////////////////////////////////////
if($_GET['code']=='00' || !$_GET['code']){

	if($haveCategory)
	{
		showlist();
	}
}
// show form addnew cat
if($_GET['code']=='01'){
	
	$tpl->newBlock("addnew_cat");
	$info['parentid1']=$pid;
	$info['parentid'] .= '<select name="parentid" style="WIDTH: 220px" >';
	$info['parentid'] .= '<option value="0">Root</option>';
	if($tree)
	foreach($tree as $k => $v) {
		foreach($v as $i => $j) {
			$selectstr='';
			if ($info['parentid1']==$k)
				$selectstr=" selected ";
			$info['parentid'] .= '<option value="' . $k . '"'.$selectstr.'>' . $j . '</option>';
		}
	}
	
	$info['parentid'] .= '</select>';
	$tpl->assign("parentid",$info['parentid']);
	$tpl->assign("thu_tu",auto_order($category,'thu_tu',$pid));
	if($pid==0){
		$tpl->newBlock("postion1");
	}
	if($haveCategory)
	{
		showlist();
	}
	
	
	
}
// up date cat 
if($_GET['code']=='02'){
	$in_name=compile_post('name');
	if ($in_name)
	{
		$a=array(
					'name'=>$in_name,
				);
		$a['thu_tu']=intval(compile_post('thu_tu'));
		$a['active']=intval(compile_post('active'));
		$a['postion']=compile_post('postion');
		$a['parentid']=intval(compile_post('parentid'));
		$b=$DB->compile_db_insert_string($a);
		$sql="INSERT INTO ".$category." (".$b['FIELD_NAMES'].") VALUES (".$b['FIELD_VALUES'].")";
		$DB->query($sql);
		message("Th&#234;m m&#7899;i Nh&#243;m th&#224;nh c&#244;ng !");
	}
	else
	{
		message("Kh&#244;ng c&#243; d&#7919; li&#7879;u &#273;&#7847;u v&#224;o ! H&#227;y th&#7917; l&#7841;i !");
	}
	if($haveCategory)
	{
		showlist();
	}
}
// show form update cat 
if($_GET['code']=='03'){
	$id=intval($_GET['id']);
	$tpl->newBlock("update_cat");
	$sql="SELECT * FROM ".$category." WHERE ".$id_category."=".$id;
	$db=$DB->query($sql);
	if($rs=mysql_fetch_array($db)){
		$tpl->assign("name",$rs['name']);
		$tpl->assign("id",$rs[$id_category]);
		$tpl->assign("thu_tu",$rs['thu_tu']);
		
		
		if($rs['active']==1){
			$tpl->assign("active","checked");
		}else {
			$tpl->assign("active","");
		}
		
		$info['parentid1']=$rs['parentid'];
		$info['parentid'] .= '<select name="parentid" style="WIDTH: 220px" >';
		$info['parentid'] .= '<option value="0">Root</option>';
		if($tree)
		foreach($tree as $k => $v) {
			foreach($v as $i => $j) {
				$selectstr='';
				if ($info['parentid1']==$k)
					$selectstr=" selected ";
				$info['parentid'] .= '<option value="' . $k . '"'.$selectstr.'>' . $j . '</option>';
			}
		}
		$info['parentid'] .= '</select>';
		$tpl->assign("parentid",$info['parentid']);
		
		$tpl->assign($rs['postion'],"selected");
	}
	if($haveCategory)
	{
		showlist();
	}
} 
// update  cat 
if($_GET['code']=='04'){
	
	$id=intval($_GET['id']);
	if ($id)
	{
		$in_name=compile_post('name');
		if ($in_name)
		{
			$a=array(
						'name'=>$in_name,
					);
			$a['parentid']=intval(compile_post('parentid'));
			$a['postion']=compile_post('postion');
			$a['thu_tu']=intval(compile_post('thu_tu'));
			$a['active']=intval(compile_post('active'));
			$b=$DB->compile_db_update_string($a);
			$sql="UPDATE ".$category." SET ".$b." WHERE ".$id_category."=".$id;
			$DB->query($sql);
			message("S&#7917;a th&#244;ng tin Nh&#243;m th&#224;nh c&#244;ng !");
		}
		else
		{
			message("Kh&#244;ng c&#243; d&#7919; li&#7879;u &#273;&#7847;u v&#224;o ! H&#227;y th&#7917; l&#7841;i !");
		}
	}
	if($haveCategory)
	{
		showlist();
	}
}
// change order, active cat 
if ($_GET['code']=='06')
{
	if ($_GET['pid'])
	{
		$dk=intval($_GET['pid']);
	}
	else
	{
		$dk=0;
	}
	$sql="Select * from ".$category." where parentid=$dk order by thu_tu desc,".$id_category." desc";
	$db=phan_trang($p,'?page='.$par_page.'&code=00&pid='.$dk,$sql,$maxpage,$maxcat,'p');
	$rs=$db['db'];
	change_active_order_paging($category,$id_category,$rs);
	showlist();
}
// delete cat 
if($_GET['code']=='05'){
	$id=intval($_GET['id']);
	if(checkDelCategory($id,$category,$id_category,$item)){
		$sql="DELETE FROM $category WHERE $id_category=$id";
		$DB->query($sql);
		message("&#272;&#227; x&#243;a th&#224;nh c&#244;ng !");
	}
	if($haveCategory)
	{
		showlist();
	}
}
///////////////// end category ////////////////////////////////////

///////////////// item /////////////////////////////////////////////
// show addnew form item
if($_GET['code']=='11'){ 
	$tpl->newBlock("addnew_item");
	$info['parentid1']=$pid;
	$info['parentid'] .= '<select name="parentid" style="WIDTH: 220px" >';
	$info['parentid'] .= '<option value="0">Root</option>';
	$k=$pid;
	if($tree)
	foreach($tree as $k => $v) {
		foreach($v as $i => $j) {
			$selectstr='';
			if ($info['parentid1']==$k)
				$selectstr=" selected ";
			$info['parentid'] .= '<option value="' . $k . '"'.$selectstr.'>' . $j . '</option>';
		}
	}
	$info['parentid'] .= '</select>';
	$tpl->assign("parentid",$info['parentid']);
	$tpl->assign("thu_tu",auto_orders($item,'thu_tu',$pid,$id_category));
	//-------
	$oFCKeditor = new FCKeditor("comment") ;
	$oFCKeditor->BasePath = '../FCKeditor/';
	$oFCKeditor->Value = $rs['comment'];
	$oFCKeditor->ToolbarSet = 'Basic';
	$oFCKeditor->Width='100%';
	$oFCKeditor->Height='150';
	$tpl->assign("comment",$oFCKeditor->CreateHtml());
	if($haveCategory)
	{
		showlist();
	}
}
// insert item
if($_GET['code']=='12') {
	$msg="";
	$in_name=compile_post('name');
	$a=array(
			'name'=>$in_name
			);
	$a['link']=compile_post('link');	
	$a[$id_category]=compile_post('parentid');
	
	$x=uploadimage('image');
	$a['image']=$x['image'];
	$a['small_image']=$x['small_image'];
	$a['normal_image']=$x['normal_image'];
	
	$a['active']=compile_post('active');
	$a['thu_tu']=compile_post('thu_tu');
	$a['target']=compile_post('target');
	$a['comment']=$_POST['comment'];
	$a['ngay_dang']=time()+$CONGIF['time_offset'];
	if (!$msg)
	{	
		$b=$DB->compile_db_insert_string($a);
		$sql="INSERT INTO ".$item." (".$b['FIELD_NAMES'].") VALUES (".$b['FIELD_VALUES'].")";
		$DB->query($sql);
		
		message("&#272;&#227; th&#234;m m&#7899;i th&#224;nh c&#244;ng !");
	}
	else
	{
		message($msg);
	}
	if($haveCategory)
	{
		showlist();
	}
}
// show form update item
if($_GET['code']=='13'){
	$msg="";
	$tpl->newBlock("update_item");
	$id=intval($_GET['id']);
	if ($id)
	{
		$sql="Select * from ".$item." where ".$id_item."=".$id;
		$a=$DB->query($sql);
		$info=array();
		if ($b=mysql_fetch_array($a))
		{
			$tpl->assign("id",$id);
			$tpl->assign("name",$b['name']);
			$info['image']="";
			if ($b['image'])
			{
				//local file
				$image_info = @getimagesize('../'.$CONFIG['upload_image_path'].$b['image']);
				if (get_file_extension($b['image'])=='swf' || get_file_extension($b['image'])=='SWF')
				{
					$info['image'].='
					  <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0" '.$image_info[3].' />
					  <param name="movie" value="../'.$CONFIG['upload_image_path'].$b['image'].'">
					  <param name="quality" value="high">
					  <param name="play" value="true">
					  <param name="scale" value="false">
					  <embed src="../'.$CONFIG['upload_image_path'].$b['image'].'" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" '.$image_info[3].'></embed></object>
					<br>';
				}
				else
				{
					$info['image'].="<img src='../".$CONFIG['upload_image_path'].$b['small_image']."' border='0' ><br>";
				}
			}
			$tpl->assign("image",$info['image']);
			$tpl->assign("link",$b['link']);
			$tpl->assign("thu_tu",$b['thu_tu']);
			if($b['active']==1)
				$tpl->assign("active","checked");
			else 
				$tpl->assign("active","");
			//-------
			$oFCKeditor = new FCKeditor("comment") ;
			$oFCKeditor->BasePath = '../FCKeditor/';
			$oFCKeditor->Value = $b['comment'];
			$oFCKeditor->ToolbarSet = 'Basic';
			$oFCKeditor->Width='100%';
			$oFCKeditor->Height='150';
			$tpl->assign("comment",$oFCKeditor->CreateHtml());
			$tpl->assign("idc",$b[$id_category]);
			$info[$id_category]=$b[$id_category];
			$info['options_catlg'] .= '<select name="parentid" style="WIDTH: 220px" >';
			$info['options_catlg'] .= '<option value="0">Root</option>';
			if($tree)
			foreach($tree as $k => $v) {
				foreach($v as $i => $j) {
					$selectstr='';
					if ($info[$id_category]==$k)
						$selectstr=" selected ";
					$info['options_catlg'] .= '<option value="' . $k . '"'.$selectstr.'>' . $j . '</option>';
				}
			}
			$info['options_catlg'] .= '</select>';
			$tpl->assign("parentid",$info['options_catlg']);
			
		}
	}
	if($haveCategory)
	{
		showlist();
	}
}
// update item
if($_GET['code']=='14'){
	if ($id)
	{
		$msg="";
		$in_name=compile_post('name');
		if ($in_name)
		{
			$a=array(
						'name'=>$in_name,
					);
			$sql="SELECT * FROM ".$item." WHERE ".$id_item."=".$id;
			$db=$DB->query($sql);
			if($rs=mysql_fetch_array($db)){
				$image=$rs['image'];
				$normal_image=$rs['normal_image'];
				$small_image=$rs['small_image'];
			}					
			if(compile_post('xoa_anh')){
				deleteimage($image,$normal_image,$small_image);
			}
			if($_FILES['image']['size']){
				deleteimage($image,$normal_image,$small_image);
				$image=uploadimage('image');
				$a['image']			=$image['image'];
				$a['normal_image']	=$image['normal_image'];
				$a['small_image']	=$image['small_image'];
			}
			$a[$id_category]		=compile_post('parentid');
			$a['thu_tu']			=compile_post('thu_tu');
			$a['link']				=compile_post('link');
			$a['active']			=compile_post('active');
			$a['target']			=compile_post('target');
			$a['comment']			=$_POST['comment'];
			if (!$msg)	{
				$b=$DB->compile_db_update_string($a);
				$sql="UPDATE ".$item." SET ".$b." WHERE ".$id_item."=".$id;
				$DB->query($sql);
				message($error."&#272;&#227; s&#7917;a ch&#7919;a th&#224;nh c&#244;ng !");
			}else{
				message($error.$msg);
			}
		}else{
			message($error."Kh&#244;ng c&#243; d&#7919; li&#7879;u &#273;&#7847;u v&#224;o ! H&#227;y th&#7917; l&#7841;i !");
		}
		showlist();
	}
}
//delete item
if($_GET['code']=='15'){
	$sql="SELECT * FROM ".$item." WHERE ".$id_item."=".$id;
	$db=$DB->query($sql);
	if($rs=mysql_fetch_array($db)){
		deleteimage($rs['image'],$rs['normal_image'],$rs['small_image']);
	}
	$sql="DELETE FROM ".$item." WHERE ".$id_item."=".$id;
	$DB->query($sql);
	message("&#272;&#227; x&#243;a xong !");
	if($haveCategory)
	{
		showlist();
	}
}
//change order active item
if($_GET['code']=='16'){
	$dklogo="";
	$dklogo="where $id_category=".intval($_GET['pid'])." ";	
	$sql="Select * from ".$item." $dklogo order by thu_tu desc,".$id_item." desc";
	$db=phan_trang($pitem,'?page='.$par_page.'&code=00&pid='.$dk,$sql,$maxpage,$maxitem,'pitem');
	change_active_order_paging($item,$id_item,$db['db']);
	showlist();
}

// delete multi check
// delete multi item
if($_GET['code']=='17'){
	//$id_items=compile_post("del1_id");
	$id_items=$_POST['delallitem'];
	if($id_items);
	$list_id=0;
	foreach($id_items as $idit){
		$list_id.=','.$idit;
	}
	$sql1="SELECT * FROM $item WHERE $id_item IN (".$list_id.")";
	$db1=$DB->query($sql1);
	while($rs1=mysql_fetch_array($db1)){
		deleteimage($rs1['image'],$rs1['normal_image'],$rs1['small_image']);
	}
	$sql="DELETE FROM $item WHERE $id_item IN (".$list_id.")";
	$db=$DB->query($sql);
	if($db) message("Đã xóa xong !");
	else message("Lỗi không xóa được !");
	
	showlist();

}

///////////////// end item ////////////////////////////////////////
function showlist()
{
	global $DB,$info,$catlgstring2,$CONFIG,$tpl,$item,$id_item,$category,$id_category,$par_page,$maxpage,$maxitem,$maxcat,$p,$pitem,$tree;
	$info['root']="<a href='main.php?page=".$par_page."&pid=0' class='link_name'>Root</a> > ";
	if ($_GET['pid'])
	{
		$dk=intval($_GET['pid']);
		$info['root'].=$catlgstring2;
	}
	else
	{
		$dk=0;
	}
	
	$tpl->newBlock("show_category");
	$sql="Select * from ".$category." where parentid=$dk order by thu_tu desc,".$id_category." desc";
	
	//$a=$DB->query($sql);
	$a=phan_trang($p,'?page='.$par_page.'&code=00&pid='.$dk,$sql,$maxpage,$maxcat,'p');
	$info=array();
	if (mysql_num_rows($a['db']))
	{
	//	$tpl->newBlock("list_category");
		$i=0;
		$tt=1;
		while ($b=mysql_fetch_array($a['db']))
		{
			
			$info[$id_category]=$b[$id_category];
			$tpl->newBlock("list_cat");
			$tpl->assign("link","?page=".$par_page."&pid=".$info[$id_category]);
			$tpl->assign("name",$b['name']);
			$tpl->assign("id",$info[$id_category]);
			$tpl->assign("thu_tu",$b['thu_tu']);
			$tpl->assign("tt",$tt++);
			if($b['id_catlg']!=2)
			{
			$tpl->assign("img","<img border='0' src='images/delete.gif' width='16' height='16' />");
			}
			$i++;
			if($i==2){
				$tpl->assign("cl","bg_row_list");
				$i=0;
			}else {
				$tpl->assign("cl","");
			}
			if($b['active']==1){
				$tpl->assign("active","checked");
				$tpl->assign("style","");			
			}else {
				$tpl->assign("active","");
				$tpl->assign("style","msg");
			}
		$tpl->assign("vitri",$b['postion']);
		$tpl->assign("id_category",$b[$id_category]);
		$tpl->assign("linkcat","?page=$par_page&pid=".$b[$id_category]);
		$tpl->assign("link_update","?page=$par_page&pid=$pid&id=$b[$id_category]&code=03&p=$p&pitem=$pitem");

		$tpl->assign("link_del","?page=$par_page&pid=$pid&id=$b[$id_category]&code=06&p=$p&pitem=$pitem");
			
		}
		$tpl->newBlock("cac_trang");
		$tpl->assign("cac_trang",$a['cac_trang']);
	}else {
		$tpl->newBlock("thongbao");
		$tpl->assign("thongbao","D&#7919; li&#7879;u ch&#432;a &#273;&#432;&#7907;c c&#7853;p nh&#7853;t !");
	}
	//logo
	
	$tpl->newBlock("show_item");
	$info['parentid1']=$pid;
	$info['parentid'] .= '<select name="parentid" style="WIDTH: 220px"  class="dropdownlist">';
	$info['parentid'] .= '<option value="0">Root</option>';
	if($tree)
	foreach($tree as $k => $v) {
		foreach($v as $i => $j) {
			$selectstr='';
			if ($info['parentid1']==$k)
				$selectstr=" selected ";
			$info['parentid'] .= '<option value="' . $k . '"'.$selectstr.'>' . $j . '</option>';
		}
	}
	if($dk==''){
		$dk="$id_category=$pid";
	}
	$info['parentid'] .= '</select>';
	$tpl->assign("parentid",$info['parentid']);
	
	$dklogo="";
	$dklogo="where $id_category=".intval($_GET['pid'])." ";			
	
	$sql="Select * from ".$item." $dklogo order by thu_tu desc,".$id_item." desc";
	$db=phan_trang($pitem,'?page='.$par_page.'&code=00&pid='.$dk,$sql,$maxpage,$maxitem,'pitem');
//	$a=$DB->query($sql);
	$info=array();
	$x=0;
	$i=0;
	$n=0;
	while ($b=mysql_fetch_array($db['db']))
	{
		$x++;
		$n++;
		$tpl->assignGlobal("n",$n);
		$tpl->newBlock("list_item");
		$tpl->assign("id",$b[$id_item]);
		$tpl->assign("thu_tu",$b['thu_tu']);
		$i++;
		if($i==2){
			$tpl->assign("cl","bg_row_list");
			$i=0;
		}else {
			$tpl->assign("cl","");
		}
		
		$tpl->assign("stt",$x);
		if($b['active']==1){
			$tpl->assign("active","checked");
			$tpl->assign("name",$b['name']);
		}else {
			$tpl->assign("active","");
			$tpl->assign("name","<font color='red'>".$b['name']."</font>");
		}
		$info['image']='';
		if ($b['image'])
		{
			//local file
			$image_info = @getimagesize('../'.$CONFIG['upload_image_path'].$b['image']);
			if (get_file_extension($b['image'])=='swf' || get_file_extension($b['image'])=='SWF')
			{
				$info['image']='
				 <br> <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0" '.$image_info[3].' />
				  <param name="movie" value="../'.$CONFIG['upload_image_path'].$b['image'].'">
				  <param name="quality" value="high">
				  <param name="play" value="true">
				  <param name="scale" value="false">
				  <embed src="../'.$CONFIG['upload_image_path'].$b['image'].'" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" '.$image_info[3].'></embed></object>
				<br>';
			}
			else
			{
				$info['image'].="<img src='../".$CONFIG['upload_image_path'].$b['small_image']."' border='0' ><br>";
			}
		}
		$info['link']="Link: <a href='".$b['link']."' target='_blank'>".$b['link']."</a><br>";
		$tpl->assign("link_edit","?page=$par_page&pid=$b[$id_category]&id=$b[$id_item]&code=13&pitem=$pitem&p=$p");
		$tpl->assign("image",$info['image']);
		$tpl->assign("link",$info['link']);
	}
	$tpl->newBlock("phan_trang");
	$tpl->assign("cac_trang",$db['cac_trang']);
	if($x<=0){
		$tpl->newBlock("thongbao1");
		$tpl->assign("thongbao","D&#432; li&#7879;u ch&#432;a &#273;&#432;&#7907;c c&#7853;p nh&#7853;t !");
	}
}
class catlg_tree{
	var $n;
	var $current_idcatlg;
	
	function catlg_tree($current_idcatlg=0)
	{
		global $item,$id_item,$category,$id_category;
		$this->current_idcatlg=$current_idcatlg;
	}
	function get_catlg_tree($parent = 0)
	{
		global $DB,$tree,$item,$id_item,$category,$id_category; // add $catlg_old
		$raw = $DB->query("select * from ".$category." where parentid='$parent' order by ".$id_category." asc");
		// add -- if it has childs
		if ($DB->get_affected_rows() > 0) {
			$this->n++;
		} else {
			return;
		} while ($result = mysql_fetch_array($raw)) {
			for($i = 0;$i < $this->n;$i++) {
				$tree[$result[$id_category]]['name'] .= '-- ';
			}
			$tree[$result[$id_category]]['name'] .= $result['name'];
			$this->get_catlg_tree($result[$id_category]);
		}
		// all childs listed, remove --
		$this->n--;
	}
	function get_catlg_string($id_catlg)
	{
		global $DB,$catlgstring,$item,$id_item,$category,$id_category;
		if ($id_catlg==0)
			return;
		else
		{
			$sql="select * from ".$category." where ".$id_catgory."=".$id_catlg;
			$a=$DB->query($sql);
			if ($b=mysql_fetch_array($a))
			{
				$catlgstring = $b['name']." > ".$catlgstring;
				$this->get_catlg_string($b['parentid']);
			}
		}
	}
	function get_catlg_string_admin($id_catlg)
	{
		global $DB,$catlgstring2,$item,$id_item,$category,$id_category,$par_page;
	
		if ($id_catlg==0)
			return;
		else
		{
			if ($this->current_idcatlg==$id_catlg)
			{
				$showclass="class='link_name'";
			}
			else
			{
				$showclass="class='link_name'";
			}
			$sql="select * from ".$category." where ".$id_category."=".$id_catlg;
			$a=$DB->query($sql);
			if ($b=mysql_fetch_array($a))
			{
				$catlgstring2 = "<a href='main.php?page=".$par_page."&pid=".$id_catlg."' $showclass >".$b['name']."</a> > ".$catlgstring2;
				$this->get_catlg_string_admin($b['parentid']);
			}
		}
	}	
}

$tpl->printToScreen();
?>